﻿$(function() {
	
});

function setFormValidation(){
	$.validate({
		form : '#orderService',
		onSuccess : function() {
			$('#resp-alert').html('Bearbeiten....');
			$('#alert').show();
			ajaxPost('#orderService',saveServiceCallback);
		  return false; // Will stop the submission of the form
		},
		validateOnBlur : true,
		scrollToTopOnError : false // Set this property to true if you have a long form
	});
	$('#save-service').click(function(){
		$('#orderService').submit();
	});
	var ini_geburtstag = $('#geburtstag_show').val();
	if( ini_geburtstag != ''){
		$('#geburtstag_show').val(moment(ini_geburtstag).format('DD/MM/YYYY'));
	}
	$('.birthday').datepicker({
		format: 'dd/mm/yyyy',
		todayHighlight: true,
		autoclose:true,
	}).on('changeDate', function(e){
		if(typeof e.date != 'undefined'){
			$('#geburtstag').val(moment(e.date.getTime()).format('YYYY-MM-DD'));
		}
    });
	$('.time').timepicker({ 
		'timeFormat': 'H:i',
		'minTime': '8:00am',
		'maxTime': '9:00pm',
		'step':'60'
	});
	
}
function saveServiceCallback(data){
	switch (parseInt(data)){
		case -99:
			$('#resp-alert').html('Pflichtfeld leer.');
			break;
		case -98:
			$('#resp-alert').html('Pflichtfeld leer.');
			break;
		case -97:
			$('#resp-alert').html('Pflichtfeld leer.');
			break;
		case -5:
			$('#resp-alert').html('Arztin nicht verf&uuml;gbar, bitter w&auml;hlen Sie ein andere Uhr.');
			break;
		case -3:
			$('#resp-alert').html('Datenbankfehler. Bitte &uuml;berprüfen Sie die Felder.');
			break;
		case 1 :
			$('#myModal').modal('toggle');
			$('#main-alert').show();
			if($('#fullCal').size() != 0){
				console.info('try to update callender');
				$('#fullCal').fullCalendar( 'refetchEvents' );
			}
			if(typeof $.notify == 'function'){
				$.notify('修改成功',"success");
			}
			break;
		default:
			$('#resp-alert').html('Interner serverfehler. Anmeldung per Email info@praxis-ju.de oder Telefon 01702778481');
	}
	$(".success-alert").show();
}

function processKundenInfo(data){
	if(data != ''){
		//console.info(JSON.parse( data ));
		var client = JSON.parse( data );
		$('#kunden_id').val(client.kunden_id);
		$('#name').val(client.name);
		$('#vorname').val(client.vorname);
		$('#geburtstag').val(client.geburtstag);
		$('#geburtstag_show').val(moment(client.geburtstag).format('DD/MM/YYYY'));
		$('#familienstand').val(client.familienstand);
		$('#anzahl_kinder').val(client.anzahl_kinder);
		$('#beruf').val(client.beruf);
		$('#address').val(client.address);
		$('#plz').val(client.plz);
		$('#land').val(client.land);
		$('#telnum').val(client.telnum);
		$('#law').prop('checked',true);
	}else{
		$('#kunden_id').val("");
		$('#name').val("");
		$('#vorname').val("");
		$('#geburtstag').val("");
		$('#geburtstag_show').val("");
		$('#familienstand').val("");
		$('#anzahl_kinder').val("");
		$('#beruf').val("");
		$('#address').val("");
		$('#plz').val("");
		$('#land').val("");
		$('#telnum').val("");
		$('#law').prop('checked',false);
	}
}

function tryDelete(jsEvent){
	var delete_p = $('#delete-event').offset()
	var delete_top = delete_p.top;
	var delete_left = delete_p.left;
	var delete_w = $('#delete-event').width();
	var delete_h = $('#delete-event').height();
	if(jsEvent.pageX >= delete_left && jsEvent.pageX <= (delete_left + delete_w) && jsEvent.pageY >= delete_top && jsEvent.pageY <= (delete_top + delete_h)){
		return true
	}
	return false;
}

function resetTime(event,dayDelta, minuteDelta, allDay, revertFunc){
	var eid = event.id;
	var start = new Date(event.start).getTime()/1000;
	var end = new Date(event.end).getTime()/1000;
	var c = confirm('确定进行操作？');
	if(c){
		$.ajax({
		   url: url + '/resetServiceTime',
		   data: {
				'tid': eid,
				'start': start,
				'end': end,
		   } ,
		   type: "POST",
		   success: function(data) {
				if(data == '1'){
					$.notify('修改成功',"success");
				}else{
					revertFunc();
				}
		   }
		});
	}else{
		revertFunc();
	}
}

function hasEmail(){
	return $('#email').hasClass('valid');
}